package com.example.war;

import java.util.Random;

import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.PointF;
import android.util.FloatMath;
import android.util.Log;

public class Hole {
    private Quad mHole;
    private Quad mSpark;
    private static final int DISPLAY_TIME = 500;
    private static final int SPARK_COUNT = 15;
    private Spark[] mSparks = new Spark[SPARK_COUNT];
    private long mStartTime;
    private int mDuration;
    
    public Hole(Context context, GL10 gl) {
        mHole = new Quad(false);
        Bitmap bitmap = Utils.loadRawImage(context.getResources(), R.raw.hole);
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        mHole.setSize(width, height);
        mHole.setTexture(gl, bitmap, false);
        bitmap.recycle();
        
        mSpark = new Quad(false);
        Bitmap sparkBmp = Utils.loadRawImage(context.getResources(), R.raw.spark);
        int sparkW = sparkBmp.getWidth();
        int sparkH = sparkBmp.getHeight();
        mSpark.setSize(sparkW, sparkH);
        mSpark.setTexture(gl, sparkBmp, false);
        bitmap.recycle();
        
        for (int i = 0; i<SPARK_COUNT; i++) {
            mSparks[i] = new Spark();
        }
    }
    
    public void reset(long time, int duration) {
        mStartTime = time;
        mDuration = duration;
        mHole.setAlpha(1);
        for (int i = 0; i < SPARK_COUNT; i++) {
            initSpark(mSparks[i], i*i);
        }
    }
    
    public void draw(GL10 gl, float x, float y) {
        long currentTime = System.currentTimeMillis();
        long elapseTime = currentTime - mStartTime;
        
        if (elapseTime > mDuration) {
            return;
        }

        gl.glPushMatrix();
        gl.glTranslatef(x, y, 0);
        
//        if (elapseTime < 300) {
//            Spark spark;
//            float pi = (float)Math.PI;
//            for (int i = 0; i < SPARK_COUNT; i++) {
//                spark = mSparks[i];
//                int t = (int) (currentTime - spark.startTime);
//                if (t < 20)
//                    continue;
//                t /= 20;
//                float dist = spark.v * t;
//                
//                if (t >= spark.dieTime) {
//                    continue;
//                }
//    
//                float radians = (float) Math.toRadians(spark.angle);
//                float distX;
//                float distY;
//                
//                
//                if (spark.angle > 0 && spark.angle <= 90) {
//                    distX = dist * FloatMath.cos(radians);
//                    distY = dist * FloatMath.sin(radians);
//                } else if (spark.angle > 90 && spark.angle <= 180) {
//                    distX = - dist * FloatMath.cos(pi - radians);
//                    distY = dist * FloatMath.sin(pi - radians);
//                } else if (spark.angle > 180 && spark.angle < 270) {
//                    distX = -dist*FloatMath.cos(radians - pi);
//                    distY = -dist*FloatMath.sin(radians - pi);
//                } else {
//                    distX = dist*FloatMath.sin(radians - pi*1.5f);
//                    distY = -dist*FloatMath.cos(radians - pi*1.5f);
//                }
//                
//                gl.glPushMatrix();
//                gl.glTranslatef(spark.position.x+distX, spark.position.y+distY, 0);
//                gl.glRotatef(spark.angle, 0, 0, 1f);
//                
//                //Log.d("vvvv", "Sparl["+i+"]: "+distX+", "+distY + "  angle:"+spark.angle);
//                //gl.glPushMatrix();
//                
//                mSpark.draw(gl, 0, 0, 0, 0, true);
//                //gl.glPopMatrix();
//                gl.glPopMatrix();
//            }
//        }
        
        if (elapseTime > DISPLAY_TIME) {
            float alpha = 1 - 1f/(mDuration - DISPLAY_TIME) * (elapseTime-DISPLAY_TIME);
            mHole.setAlpha(alpha);
            Log.d("vvvv","setAlpha:"+alpha);
        }
        
        //gl.glTranslatef(-70, -30, 0);
        mHole.draw(gl, 0, 0, 0, 0, true);
        gl.glPopMatrix();
    }
    
    private void initSpark(Spark spark, int i) {
        long currentTime = System.currentTimeMillis();
        Random random = new Random(currentTime);
        
        random.setSeed(currentTime+i);
        spark.startTime = currentTime + random.nextInt(200);
        random.setSeed(currentTime+i*4);
        spark.v = 12 + random.nextInt(40);
        int angle = random.nextInt(360);
        spark.angle = angle;
        spark.active = true;
        spark.isDie = false;
        spark.dieTime = currentTime + 300 + random.nextInt(mDuration-100);
        
        random.setSeed(currentTime+i*5);
        spark.position.x = -40 + random.nextInt(80);
        random.setSeed(currentTime+i*6);
        spark.position.y = -40 + random.nextInt(80);
    }
    
    class Spark {
        long startTime;
        PointF position = new PointF();
        float v;
        float a;
        float angle;
        long dieTime;
        boolean active;
        boolean isDie;
    }
}
